<table id="user_data"></table>

<!-- <script type="text/javascript" src="back/js/jquery-1.9.1.js"></script> -->
<script>
	var obj;
	var editRow=undefined;//记录当前正在被编辑的行
	var currentOp;
	var flag;
	
	obj=$('#user_data').datagrid({
		url:'../back/user/select/0',
		fitColumns:true,
		striped:true,
		loadMsg:"数据加载中...",
		pagination:true,
		fit:true,
		pageNumber:1,
		pageSize:10,
		pageList:[10,15,20,25,30],
		sortName:'userid',
		remoteSort:false,
		columns:[[
			{field:'userids',title:'',width:100,align:'center',checkbox:true},
			{field:'userid',title:'用户编号',width:100,align:'center',sortable:true},
			{field:'uname',title:'用户名',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'uage',title:'年龄',width:100,align:'center',editor:{type:'numberbox',option:{requires:true}}},
			{field:'usex',title:'性别',width:100,align:'center',
				formatter:function(value){ 
                    if(value=='1'){
                        return '男';
                    }else{
                        return '女'
                    } 
                },
                editor:{
                    type:"combobox",
                    options:{
                        data:[{'key':'男','value':'1'},{'key':'女','value':'2'}],
                        valueField: 'value',    
                        textField: 'key',    
                        panelHeight: 'auto',  
                        required: true ,  
                        editable:false 
                    }
                }
            },
			{field:'tel',title:'电话',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'uaccount',title:'总数',width:100,align:'center',editor:{type:'numberbox',option:{requires:true}}},
			{field:'upwd',title:'密码',width:100,align:'center',editor:{type:'text',option:{requires:true}},
				formatter:function(value,row,index){  
                   return '**********';
                }  
            },
			{field:'uimage',title:'头像',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'address',title:'地址',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'createdate',title:'创建日期',width:100,align:'center',
				formatter:function(value,row,index){  
                    var Timestamp = new Date(value);  
                    return Timestamp.toLocaleString();  
                }  
			},
			{field:'createperson',title:'创建人',width:100,align:'center'},
			{field:'updatedate',title:'修改日期',width:100,align:'center',
				formatter:function(value,row,index){  
                    var unixTimestamp = new Date(value);  
                    return unixTimestamp.toLocaleString();  
                }  		
			},
			{field:'updateperson',title:'修改人',width:100,align:'center'},
			{field:'delflag',title:'是否删除',width:100,align:'center',hidden:true},
			{field:'status',title:'状态',width:100,align:'center',
				formatter:function(value,row,index){  
                    if(value=='1'){
                        return '有效';
                    }else{
                        return '禁用'
                    }
                },
                editor:{
                    type:"combobox",
                    options:{
                        data:[{'key':'有效','value':'1'},{'key':'禁用','value':'0'}],
                        valueField: 'value',    
                        textField: 'key',    
                        panelHeight: 'auto',  
                        required: true ,  
                        editable:false 
                    }
                }
            }
		]],
		toolbar:[{
			text:"添加",
			iconCls:"icon-add",
			handler:function(){
				flag="添加";
				currentOp="insert";//currentOp  是一个全局的变量
				if (editRow != undefined) {
                    $("#user_data").datagrid('endEdit', editRow);
                }
                if (editRow == undefined) {
                    $("#user_data").datagrid('insertRow', {
                        index: 0,
                        row: {}
                    });
                    $("#user_data").datagrid('beginEdit', 0);
                    editRow = 0;
                }
			}
		},{
			text:'修改',
			iconCls:"icon-edit",
			handler:function(){
				currentOp="update";
				flag="修改";
				var rows=obj.datagrid("getChecked")[0];
				if(rows!=undefined){
					if(editRow!=undefined){//说明有行正在被编辑
						obj.datagrid("rejectChanges");//回滚所有数据
						obj.datagrid("endEdit",editRow);//关闭正在被编辑的行
						editRow=undefined;
					}else{
						//获取用户选中的行的索引
						var index=obj.datagrid("getRowIndex",rows);
						//console.info(rows);
						obj.datagrid("updateRow",{index:index,row:rows});
						obj.datagrid("beginEdit",index);
						editRow=index;//记录当前被编辑的行的索引
					}
				}else{
					$.messager.show({title:'温馨提示',msg:'请选中您要修改的数据.....',timeout:2000,showType:'slide'});
				}
			}
		},{
			text:'删除',
            iconCls:"icon-remove",
            handler:function(){
                var rows=obj.datagrid("getChecked");
                if(rows[0]!=undefined){
                    $.messager.confirm('信息确认', '您真的要删除选中的数据吗?', function(rs){
                        if (rs){
                            var userids="";
                            for(var i=0;i<rows.length;i++){
                               userids += rows[i].userid+",";
                            }
                            $.ajax({
                                  type: 'DELETE',
                                  url: "../back/user/del/"+userids,
                                  dataType: "JSON",
                                  success: function(data){
                                      if(data.code=='0000'){
                                          //成功
                                          $.messager.show({title:'成功提示',msg:'用户标签删除成功....',timeout:2000,showType:'slide'});
                                        rows=null;
                                        obj.datagrid("reload");//刷新表格
                                      }else{
                                          $.messager.alert('失败提示','用户标签删除','error');
                                      }
                                  }
                            }); 
                        }else{
                            return ;
                        }
                    });
                }else{
                    $.messager.show({title:'温馨提示',msg:'请选中您要修改的数据.....',timeout:2000,showType:'slide'});
                }
                    
            }
		},{
			text:'保存',///从这里作为入口，两个操作   增加和  编辑的时候，都有一个currentop之类的
			iconCls:"icon-save",
			handler:function(){
				//先关闭编辑
				obj.datagrid("endEdit",editRow);
				//获取正在被编辑的数据
				var rows=obj.datagrid("getChanges");
				//alert(rows);
				if(rows==undefined){//说明没有被保存的数据
					obj.datagrid("rejectChanges");//回滚所有数据
					obj.datagrid("endEdit",editRow);//关闭正在被编辑的行
					editRow=undefined;
				}else if(rows.uname==""){
					editRow=undefined;
					obj.datagrid("rejectChanges");
				}else{
					//保存到数据库 
					//rows["op"]=currentOp;
					var userid=$(rows).attr("userid");//这里用$()将一个js对象包裹起来，将js对象转换成一个jquery对象
					//jquery对象--》js对象    ===》1、$("#div").eq(0);  2、$("#div")[0]    
					//alert(rows);
					var uname=$(rows).attr("uname");
					var uage=$(rows).attr("uage");
					var usex=$(rows).attr("usex");
					var tel=$(rows).attr("tel");
					var uaccount=$(rows).attr("uaccount");
					var upwd=$(rows).attr("upwd");
					var uimage=$(rows).attr("uimage");
					var address=$(rows).attr("address");
					var createdate=$(rows).attr("createdate");
					var createperson=$(rows).attr("createperson");
					var updatedate=$(rows).attr("updatedate");
					var updateperson=$(rows).attr("updateperson");
					var delflag=$(rows).attr("delflag");
					var status=$(rows).attr("status");
					$.ajax({
						  type: 'POST',
						  url: "../back/user/"+currentOp,
						  dataType: "JSON",
						  data: {"userid":userid,"uname":uname,"uage":uage,"usex":usex,"tel":tel,"uaccount":uaccount,"upwd":upwd,"uimage":uimage,
								"address":address,"createdate":createdate,"createperson":createperson,"updatedate":updatedate,"updateperson":updateperson,
								"delflag":delflag,"status":status},
						  success: function(data){
							  if(data.code=='0000'){
								  $.messager.show({
										title:'成功提示',
										msg:'用户'+flag+'成功....',
										timeout:2000,
										showType:'slide'
									});
									obj.datagrid("rejectChanges");//回滚所有数据
									rows=null;
									editRow=undefined;
									obj.datagrid("endEdit",editRow);//关闭正在被编辑的行
									obj.datagrid("reload");//刷新表格
							  }else{
								  $.messager.alert('失败提示','用户'+flag+'失败....','error');
							  }
						 }
					});
				}
			}
		},{
			text:'撤销',
			iconCls:"icon-redo",
			handler:function(){
				obj.datagrid("rejectChanges");//回滚所有数据
				obj.datagrid("endEdit",editRow);//关闭正在被编辑的行
				editRow=undefined;	
			}
		}]
	});
	 $('#user_data').datagrid('getPager').pagination({  
         pageSize: 10,  
         pageNum1: 1,  
         pageList: [10, 20, 30, 40, 50],  
         beforePageText: '第',//页数文本框前显示的汉字   
         afterPageText: '页    共 {pages} 页',  
         displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录', 
	 });
	
</script>
